package jp.sourceforge.akjrcp.text_douga.item;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;

import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;

import jp.sourceforge.akjrcp.text_douga.DougaPreference;
import jp.sourceforge.akjrcp.text_douga.DougaSystem;
import jp.sourceforge.akjrcp.text_douga.ImageObject;
import jp.sourceforge.akjrcp.text_douga.utils.GifAnimeUtils;

import com.sun.imageio.plugins.gif.GIFImageMetadata;
import com.sun.imageio.plugins.gif.GIFImageReader;

/*
 * normal image
 */
public class GifAnimeItem extends ImageItem{

private GIFImageReader reader;
	

private int loop=1;

public String toString(){
	return "GifAnimeItem:loop="+loop+" "+super.toString();
}

private GIFImageReader getGifReader(){
	if(reader!=null){
		return reader;
	}
	
	reader=GifAnimeUtils.getGifReader(new File(inputPath));
    return reader;
}


	

	public boolean writeFrame(DougaPreference preference,ImageObject baseImage,int frame) throws IOException{
		boolean written=false;
		double animationTime=0;
	
		double frameTime=getFrameTime(frame,preference.getFps());
		
		long animeDuration=(long) Math.max((GifAnimeUtils.getGifDuration(getGifReader())*loop), (double)duration);
		if(frameTime>=getStartTime() && frameTime<getStartTime()+animeDuration){
			log.info("intime:"+frameTime+" start="+getStartTime()+" end="+(getStartTime()+GifAnimeUtils.getGifDuration(getGifReader())));
			written=true;
			animationTime=frameTime-getStartTime();
			}else if(frameTime>=getStartTime() && loop==DougaSystem.FOREEVER){
				//even if foreever take care of start time
				written=true;
				animationTime=frameTime-getStartTime();
			}
		
		
		if(written==true){
			//drawing;
			
			int f=GifAnimeUtils.findAnimationFrame(getGifReader(),animationTime);
			log.warn("index="+f+" animationFrame="+animationTime);
			BufferedImage img=getGifReader().read(f);//memory is ok?
			Rectangle drawRect=getDrawRect(preference,img.getWidth(),img.getHeight());
			log.info(drawRect);
			log.info(img.getWidth()+"x"+img.getHeight());
			Graphics2D g2=(Graphics2D) baseImage.getImage().getGraphics();
			
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getTransparent()));//use tp!
			int mx=getMovedX(preference,frameTime);
			int my=getMovedY(preference,frameTime);
			
			if(frame==1){
				System.out.println("mx:"+mx+",my="+my);
			}
			g2.drawImage(img, drawRect.x+mx, drawRect.y+my, drawRect.x+drawRect.width+mx, drawRect.y+drawRect.height+my, 0, 0, img.getWidth(), img.getHeight(), null);
		
		}
		
		return written;
	}


	public Rectangle getDrawRect(DougaPreference preference,int w,int h){
		
		
		
		int dw=getWidth();
		int dh=getHeight();
		if(dw==0){
			dw=w;
		}
		if(dh==0){
			dh=h;
		}
		int dx=0;
		if(getAlign()==NONE){
			dx=getX();
		}else{
			if(getAlign()==LEFT){
				dx=0;
			}else if(getAlign()==RIGHT){
				dx=preference.getWidth()-dw;
			}else if(getAlign()==CENTER){
				dx=(preference.getWidth()-dw)/2;
			}else{
				log.warn("invalid align:"+getAlign());
			}
		}
		int dy=0;
		if(getValign()==NONE){
			dy=getY();
		}else{
			if(getValign()==TOP){
				dy=0;
			}else if(getValign()==BOTTOM){
				dy=preference.getHeight()-dh;
			}else if(getValign()==MIDDLE){
				dy=(preference.getHeight()-dh)/2;
			}else{
				log.warn("invalid valign:"+getValign());
			}
		}
		return new Rectangle(dx,dy,dw,dh);
		
	}

	public int getLoop() {
		return loop;
	}

	public void setLoop(int loop) {
		this.loop = loop;
	}
	
	public double getLastTime() {
		if(duration!=DougaSystem.FOREEVER){
			return getStartTime()+GifAnimeUtils.getGifDuration(getGifReader())*loop;
		}
		return 0;
	}

}
